package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    SetmealMapper setmealMapper;

    @Autowired
    SetmealDishMapper setmealDishMapper;


    public PageResult pageSetmeal(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page=setmealMapper.pageSetmeal(setmealPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }


    public void addSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.addSetmeal(setmeal);

        Long setmealId=setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes==null||setmealDishes.isEmpty())return;
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealId);
            setmealDishMapper.addSetmealDish(setmealDish);
        }
    }


    public SetmealVO getSetmealById(Long id) {
        SetmealVO setmealVO=new SetmealVO();
        Setmeal setmeal=setmealMapper.getSetmealById(id);
        BeanUtils.copyProperties(setmeal,setmealVO);

        List<SetmealDish> setmealDishes=setmealDishMapper.getDishBySetmealId(id);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }


    public void updateSetmeal(SetmealDTO setmealDTO) {
        Long setmealId =setmealDTO.getId();
        setmealDishMapper.deleteBySetmealId(setmealId);

        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.update(setmeal);

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealId);
            setmealDishMapper.addSetmealDish(setmealDish);
        }
    }


    public void startOrStop(Integer status, Long id) {
        if (Objects.equals(status, StatusConstant.ENABLE)){
           List<Integer> statuses = setmealDishMapper.getDishStatus(id);
           for (int dishStatus:statuses){
               if (Objects.equals(dishStatus,StatusConstant.DISABLE)) {
                   throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ENABLE_FAILED);
               }
           }
        }
        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        setmealMapper.update(setmeal);
    }

    //删除套餐
    //删除套餐下的关联菜品
    public void deleteSetmeal(List<Long> ids) {
        ids.forEach(id->{
            Setmeal setmealById = setmealMapper.getSetmealById(id);
            if (Objects.equals(setmealById.getStatus(), StatusConstant.ENABLE)){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        });
        setmealDishMapper.deleteBySetmealIds(ids);
        setmealMapper.delete(ids);
    }


    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }


    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
